package com.bestcem.xm.script.controller;

import com.bestcem.xm.common.core.domain.web.ResultJson;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.script.service.InitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 初始化脚本
 * @date 2021-11-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/scripts")
public class InitController {

    /**
     * 初始化服务
     */
    @Autowired
    private InitService initService;

    /**
     * 初始化
     *
     * @return
     */
    @AuthIgnore
    @GetMapping("/init/{org_code}")
    public ResultJson<String> init(
            @PathVariable(name="org_code", required = false)
            @NotBlank(message = "租户代号不能为空")
            @Size(max = 40, message = "租户代号不能超过40个字符") String orgCode,
            @RequestParam(name = "secret", required = false)
            @NotBlank(message = "密钥不能为空")
            @Size(max = 40, message = "密钥不能超过40个字符") String secret) {
        ServiceResult<String> result = initService.init(secret, orgCode);
        return ResultJson.handelServiceResult(result);
    }
}
    